<?php 
namespace app\socket\controller;
 use \Workerman\Worker;
 use \Workerman\Lib\Timer;
 use \GatewayWorker\Lib\Gateway;
 use app\common\library\Daili;
 use app\common\library\Game;
 use think\Db;
 use think\Cache;
  use think\Debug;
 use extend\org;
class Bqroom
{
   /* public $client_id;
    private $userid=0;
    public static $rcache;
    //*/
    public static  function onConnect($client_id=''){

       echo "\n onConnect------------------\n".$client_id;

    }

    //
    public static  function onWorkerStart($worker){
      global $game;
      $game=new Game();
      echo "\n onWorkerStart".$worker->id;
      if ($worker->id==0) {
 
         Timer::add(1, function(){
            self::loop_game();

          });
           Timer::add(1, function(){
            self::robot_game();

          });
          Timer::add(10, function(){
            self::fix_game();

          });
         self::init_game();
      }
      
      //
    }
    //
    public static  function onClose($client_id){
      global $xredis;
        echo "\n onClose:".$client_id;
    }
  //定時器不断查询
    public static  function onmessage($client_id, $message_data){
      return;
      
    }
   private static function robot_game(){
    global $xredis;
    global $game;
   $pay_money=array(10,20,30,50,100,500,1000,2000);
   $pei_money=array(18,36,54,90,180,900,1800,2600);
   $name_list=array('高VS','伍万之','刘大师','Jxsm慢','请烟谈','佳木斯帅','NIjei','摩卡咖啡','如梦林','大耳机','哦哦哦','帝王蟹');
    for($key=1;$key<=12;$key++) {
        $pay=mt_rand(0,7);
        $kai=mt_rand(0,10);
        $map[$key]["_id"]= md5(0);
        $map[$key]["__v"]= 0;
        $map[$key]["pay"]=  $pay_money[$pay];
        $map[$key]["buytime"]= time()-30-mt_rand(1,30);
        $map[$key]["unionid"]= "";
        $map[$key]["userId"]=  1;
        $map[$key]["nickname"]=$name_list[$kai];
        $map[$key]["headimgurl"]= '';
        $map[$key]["restmoney"]= 38.72;
        $map[$key]["first"]= 0;
        $map[$key]["win"]= 0;
        $map[$key]["read"]= 0;
        $map[$key]["yongMoney"]= 0;
        $map[$key]["isControl"]= 0;
        $map[$key]["creattime"]= "2019-03-13T14:01:39.654Z";
        $map[$key]["mode"]=30;
        $map[$key]["now"]=$xredis->get($game->prefix.'_now_val')/46400;
        $map[$key]["will"]=  $xredis->get($game->prefix.'_now_val')/46400;
       $xx= mt_rand(1,3);
        $map[$key]["result"]=$xx==1?0:1;
        $map[$key]["buyDirection"]= mt_rand(1,2);
        $map[$key]["state"]= 0;
        $map[$key]["isbot"]= 0;
        $map[$key]["heyueindex"]= 1;
        $map[$key]["heyue"]= "BTC";
    }
    $xredis->set($game->prefix.'str_robot',json_encode($map));
    return;
   }
     //修复程序，数据，赔付
     public static  function fix_game(){
     // echo "\nfix_game-------------------->";
      self::fixempty();
      $where['result']=array('neq','');
      $where['status']=0;
      $list=Db::name('biquan_dat')->where($where)->select();
      if ($list) { 
             foreach ($list as $key => $value) {
                self::peifu($value);
             }
      }
      return;
     }
  private static function fixempty(){
    global $xredis;
    global $game;
    $addon=json_decode($xredis->get($game->prefix.'Biquan'),true);
    $where['result']=array('eq','');
    $where['now']=array('gt',0);
    $where['createtime']=array('lt',(time()-65));
      $list=Db::name('biquan_dat')->where($where)->select();
      if ($list) { 
         foreach ($list as $key => $value) {
              $rand_val=mt_rand(1,9)*46400;

             if ($value['ifkill']==2) {
                if ($value['buyDirection']==1) {
                   $request=$value['now']+$rand_val;
                } else {
                   $request=$value['now']-$rand_val;
                }
             } else {
                  //必死
                  if ($value['ifkill']==1) {
                    if ($value['buyDirection']==1) {
                       $request=$value['now']-$rand_val;
                    } else {
                       $request=$value['now']+$rand_val;
                    }
                  }else{
                    $kill=0;
                    if ($addon['waterlever']<=$value['pay']*1.85) {
                      $kill=1;
                    }else{
                      //落入几率
                      $x=mt_rand(1,100);
                      if ($addon['percent']>=$x&&$addon['ifkeep']==1) {
                         $kill=1;
                      }
                    }
                    //输
                    if ($kill==1) {
                      if ($value['buyDirection']==1) {
                         $request=$value['now']-$rand_val;
                      } else {
                         $request=$value['now']+$rand_val;
                      }
                    }else{
                        if ($value['buyDirection']==1) {
                           $request=$value['now']+$rand_val;
                        } else {
                           $request=$value['now']-$rand_val;
                        }
                    }
                  }

             }

             $request=intval($request/46400)*46400;
          Db::name('biquan_dat')->where('id='.$value['id'])->setfield('result',$request);   
         }
         }
      return;
}
   //定時器不断查询
    public static  function loop_game(){
    global $xredis;
    global $game;
    $addon=json_decode($xredis->get($game->prefix.'Biquan'),true);
    $temp_set= $xredis->get($game->prefix.'_now_time');
    $now_time=time()*1000;
    if ($temp_set!=$now_time) {
       $xredis->set($game->prefix.'_now_time',$now_time);//用于加强计时器的健壮  
       $basedata=$xredis->get($game->prefix.'basedata');
       $isbuy=0;
       $start_time=0;
       $start_val=0;
       $start_type=0;
       $kill=0;
       $dda=$xredis->lRange($game->prefix.'biquan'.'_order_line',-1,-1);//最后一个
       if ($dda) {
            $last_dat=json_decode($dda[0],true);
            $start_time= $last_dat['buytime'];//$xredis->get('start_time');//最近有人押注的时间点
            $start_val=$last_dat['now'];//$xredis->get('start_val');//该时间的起始值
            //根据这个时间押注涨跌的大小判断目标应该涨还是跌
            //查下有没有押注
            $nowdat=$xredis->get($game->prefix.'setorder'.$start_time);

            if ($nowdat) {
              $dat=json_decode($nowdat,true);
              //押注涨的数量比跌的多
              if ($dat['up_val']>$dat['down_val']) {
                 $start_type=1;//控制目标跌
              }else{
                 $start_type=0;//控制目标涨
              }
              $kill=$dat['ifkill'];
            }  
       }
      //start_time最后一次时间点。start_val：
      $val=self::ganrao($start_time,$start_val,$start_type,$now_time,$kill);
      if (!$val) {
         return "\nwarming:empty val!";
      }
      $dd['val']=$val;
      $dd['time']=$now_time;
      //是否弹出
      $pop_time=$xredis->get($game->prefix."_poptime");
      if ($now_time==$start_time||$now_time==$pop_time) {
        Debug::remark('begin');
          $isbuy=1;
          $where['buytime']=$now_time;
          $rel=Db::name('biquan_dat')->where($where)->setfield('result',$dd['val']);
          if ($rel) {  
           $where['buytime']=$now_time;
           $list=Db::name('biquan_dat')->field('id,now,result,pay,uid,ifkill,buyDirection')->where($where)->select();
           if ($list) {
               foreach ($list as $key => $value) {
                  self::peifu($value);
              }
           }
          }
        $time=  Debug::getRangeTime('begin','end');
       }
      $xredis->lpush($game->prefix.'bic_data',json_encode($dd));
      self::BitcoinTop();
      self::psgeorder();
      self::topOrder();
      $xredis->ltrim($game->prefix.'bic_data',0,99);
      //小于等于当前时间戳的数据删掉
      if (($start_time)<=$now_time&&$start_time>0) {
        //再订单判断的时候.如果第二个压的大于第一个,那么第一个pop出,以第二个为准
        if($pop_time) {
          $xredis->set($game->prefix."_poptime",0);
        }else{
          $xredis->RPOP($game->prefix.'biquan'.'_order_line');//Pop出来,读取删除
        }
        $xredis->del($game->prefix.'setorder'.$start_time);
      }

      //debug
    }
   
      return ;
    } 
private static function BitcoinTop(){ 
  global $xredis;
  global $game;
  $map=array();
  $m1=array();
  $dat=$xredis->lRange($game->prefix.'bic_data', 0, -1);
   if ($dat) {
    foreach ($dat as $key => $str) {
      $dd=json_decode($str,true);
    //   $map[$key]["_id"]= md5($dd['time']);
    //   $map[$key]["__v"]= 0;
    //   $map[$key]["time"]= $dd['time'];
    //   $map[$key]["price"]= round($dd['val']/46400,5);
    //   $map[$key]["creattime"]= "2019-03-13T13:58:46.152Z";
    //   $map[$key]["state"]= 0;
      $map[$key]["timestamp"]= $dd['time'];
      $map[$key]["value"]= round($dd['val']/46400,4)*10000;
   }
   //-----------一分钟线
    // $m1data=array();
    // $daodata=array();
    // krsort($dat);
    // foreach ($dat as $key => $vv){
    //     $daodata[count($dat)-$key-1]=$vv;
    // }
    // $m1time=0;
    // $m1key=0;
    // $m1test=array();
    // foreach ($daodata as $key => $vv){
    //     if(count($daodata)>$m1time){
    //         $daod=json_decode($vv,true);
    //         $daov=round($daod['val']/46400,4)*10000;
    //         if($key==0){
    //             $m1time=5;
    //             $m1data[$m1key]['open']=$daov;
    //         }
    //         array_push($m1test,$daov);
    //         if($m1time==$key){
    //             arsort($m1test);
    //             $m1data[$m1key]['high']=$m1test[0];
    //             $m1data[$m1key]['low']=$m1test[4];
    //             $m1data[$m1key]['close']=$daov;
    //             $m1data[$m1key]['timestamp']=$daod['time'];
    //             $m1time+=5;
    //             $m1test=array();
    //             $m1key++;
    //             if(count($daodata)>$m1time){
    //             $m1data[$m1key]['open']=$daov;
    //             }
    //         }
    //     }
    // }
    // $xredis->set($game->prefix.'BitcoinTopm1',json_encode($m1data));
    //-----------一分钟线
    //$m1data
    
   $xredis->set($game->prefix.'BitcoinTop',json_encode($map));
  }
return;
}

private static function psgeorder(){
  global $xredis;
  global $game;
    $str=$xredis->get($game->prefix.'psgeorder');
    if (empty($str)) {
        $where=1;
        $list=Db::name('biquan_dat')->where($where)->limit(50)->order('id desc')->select();
        $xredis->set($game->prefix.'psgeorder',json_encode($list),60);
    }else{
       $list=json_decode($str,true);
    }

  $map["total"]= 300;
  $map["index"]= 1;
  $map["size"]= 300;
  $map["pageSum"]= 1;
  $data=array();
  $datax=array();
  $addon=json_decode($xredis->get($game->prefix.'Biquan'),true);
  if ($list) {
    $basedata=$xredis->get($game->prefix.'basedata');
    foreach ($list as $key => $value) {
      $user=self::get_user($value['uid']);
        $data[$key]["_id"]= md5($value['id']);
        $data[$key]["__v"]= 0;
        $data[$key]["pay"]=  intval($value['pay']/6.8);
        $data[$key]["buytime"]= $value['buytime'];
        $data[$key]["unionid"]= "";
        $data[$key]["userId"]=  $value['uid'];
        $data[$key]["nickname"]= $user['nickname'];
        $data[$key]["headimgurl"]= $user['avatar'];
        $data[$key]["restmoney"]= 38.72;
        $data[$key]["first"]= 0;
        $data[$key]["win"]= 0;
        $data[$key]["read"]= 0;
        $data[$key]["yongMoney"]= 0;
        $data[$key]["isControl"]= 0;
        $data[$key]["creattime"]= "2019-03-13T14:01:39.654Z";
        $data[$key]["mode"]= $value['mode'];
        $data[$key]["now"]= $value['now']/46400;
        $data[$key]["will"]=  $value['result']/46400;
        $data[$key]["result"]= $value['peifu']>0?1:0;
        $data[$key]["buyDirection"]= $value['buyDirection'];
        $data[$key]["state"]= 0;
        $data[$key]["isbot"]= 0;
        $data[$key]["heyueindex"]= 1;
        $data[$key]["heyue"]= "BTC";

    }
  }

  $xmap=json_decode($xredis->get($game->prefix.'str_robot'),true);
 if ($xmap&&$addon['robot']==1) {
   $data=array_merge($xmap,$data);
 } 
  $map["data"]=$data;
  $xredis->set($game->prefix.'str_psgeorder',json_encode($map));
  return;
}
private static  function get_user($uid=0,$force=0){
  global $xredis;
  global $game;
 if ($uid>0) {
   $user=json_decode($xredis->get($game->prefix.'user'.$uid),true);
   if (!$user||$force) {
      $user=Db::name('user')->field('nickname,avatar')->where('id='.$uid)->find();
      if (!$user) {
        $user['nickname']='游客';
        $user['avatar']='http://thirdwx.qlogo.cn/mmopen/vi_32/SqdZeNRC0cOkbjnXQ6Sab2gU1FYzqF0Kn18dDHoD9rftA1Mh7O7lkmgGZO1Gl9LKAyEGFO6SBn6rP5enWaYxlw/132';
      } 
      $xredis->set($game->prefix.'user'.$uid,json_encode($user));
   }
 }
 return $user;
}
private static function topOrder(){
    global $xredis;
    global $game;
    $str=$xredis->get($game->prefix.'topOrder10');
    if (empty($str)) {
        $where['peifu']=array('gt',0);
        $list=Db::name('biquan_dat')->where($where)->limit(50)->order('id desc')->select();
        $xredis->set($game->prefix.'topOrder10',json_encode($list),60);
    }else{
       $list=json_decode($str,true);
    }
 $map=array();
 $dat=array();
 if ($list) {
      foreach ($list as $key => $value) {
        $user=self::get_user($value['uid']);
        $map[$key]["_id"]=md5($value['id']);
        $map[$key]["__v"]=0;
        $map[$key]["pay"]=$value['pay']/6.8;
        $map[$key]["buytime"]=$value['buytime'];
        $map[$key]["unionid"]="";
        $map[$key]["userId"]=$value['uid'];
        $map[$key]["nickname"]=$user['nickname'];
        $map[$key]["headimgurl"]=$user['avatar'];
        $map[$key]["restmoney"]=20;
        $map[$key]["first"]=0;
        $map[$key]["win"]=0;
        $map[$key]["read"]=0;
        $map[$key]["yongMoney"]=0;
        $map[$key]["isControl"]=0;
        $map[$key]["creattime"]="2019-03-13T14:04:05.696Z";
        $map[$key]["mode"]=$value['mode'];
        $map[$key]["now"]=round($value['now']/46400,5);
        $map[$key]["will"]=round($value['result']/46400,5);
        $map[$key]["result"]=$value['peifu']>0?1:0;
        $map[$key]["buyDirection"]=$value['buyDirection'];
        $map[$key]["state"]=0;
        $map[$key]["isbot"]=0;
        $map[$key]["heyueindex"]=1;
        $map[$key]["heyue"]="BTC";

    }
 }
 $xmap=json_decode($xredis->get($game->prefix.'str_robot'),true);
 if ($xmap) {
   $okmap=array_merge($xmap,$map);
 }else{
   $okmap=$map;
 }
 $xredis->set($game->prefix.'str_topOrder',json_encode($okmap));
 return;
}
 private static function peifu($value=''){
   global $xredis;
   global $game;
   $peifu=0;
   $status=0;
   if ($value['ifkill']==2) {
     $status=1;
     $peifu=$value['pay']*1.85;
   }else{
     //买涨
     if ($value['buyDirection']==1) {
        if ($value['result']>$value['now']) {
           $peifu=$value['pay']*1.85;
           $status=1;
        }else{
           $status=2;
        }
     }else{
        //买涨
        if ($value['result']<$value['now']) {
           $peifu=$value['pay']*1.85;
           $status=1;
        }else{
           $status=2;
        }
     }
   }
      $map['status']=$status;
      $map['peifu']=$peifu;
      $where['status']=0;
      $where['id']=$value['id'];
      Db::name('biquan_dat')->where($where)->update($map);
      if ($peifu>0) {
        Db::name('user_relation')->where('uid='.$value['uid'])->setInc('peifu',$peifu);
          Db::name('user')->where('id='.$value['uid'])->setInc('point',$peifu);
          $todaystr=strtotime(date('Ymd'));
          $cmap['uid']=$value['uid'];
          Db::name('user_relation')->where($cmap)->setInc('allout',$peifu);
          $cmap['createtime']=$todaystr;
          Db::name('user_count')->where($cmap)->setInc('allout',$peifu*100);
      }
   $xredis->delete($game->prefix.'str_psgeMeOrder'.$value['uid']);

  return;
 }
 //起始时间，起始值，控制涨跌0涨：type=0控制未来标涨 type=1控制未来跌 $kill
 private static function ganrao($start_time='',$start_val='',$type=0,$now_time=0,$kill=0){
  global $game;
    $now_time=time();
    global $xredis;
    $basedata=$xredis->get($game->prefix.'basedata');
    //echo "\nbasedata:".$basedata;
    $addon=json_decode($xredis->get($game->prefix.'Biquan'),true);

    $rand_val=mt_rand(1,9);//大波震荡
    $xrand_val=mt_rand(1,9);//小波震荡

  $now_val=$xredis->get($game->prefix.'_now_val');//当前数值
  if ($start_val>0) {
     $start_baset=($start_val-$addon['basedata']);
  }else{
     $start_baset=0;
  } 
  //中间数据
    //当前时间落在：starttime--->start_+30s之间 进入则必控
  if (($start_time)>=$now_time&&$start_time>0&&$kill>0) {
           if ($kill==2) {//2控赢，1控输
              if ($type==0) {
                $type=1;
              } else {
                $type=0;
              }
           } 
           $left_time=$start_time-$now_time;

           ///////////////////////////////
            if ($type==0) {
               echo "\nkon zhang-----:".$left_time."------->".round($now_val-$start_val,5);
               $zhang_cha=round($now_val-$start_val,5);
              //如果当前值小于目标值则升
              if ($now_val<($start_val-$rand_val)) {
                $nowbase=self::zhandie($basedata,2,$rand_val); 
              }else{
                if ($zhang_cha>($left_time*2+10)) {
                     //大于20偏太高，回调
                     $nowbase=self::zhandie($basedata,1,$rand_val); 
                }else{

                    $nowbase=self::zhandie($basedata,2,$rand_val); 
                }
              }
            }else{
                echo "\nkon die-----:".$left_time."------->".round($now_val-$start_val,5);
                $die_cha=round($now_val-$start_val,5);
                //如果当前值大于目标值则跌
                if (($now_val+$rand_val)>$start_val) {
                     $nowbase=self::zhandie($basedata,1,$rand_val); 
                }else{
                  if ($die_cha<=($left_time*-2)-10) {//小于120偏太低，回调
                     $nowbase=self::zhandie($basedata,2,$rand_val);
                  }else{
                     $nowbase=self::zhandie($basedata,1,$rand_val);
                  }
                                
                }
            }        
  }else{
     $nowbase=self::zd_rand(0,$basedata);
  }
  if (!$nowbase) {
    echo 'no nowbase';
    return;
  }
  $xredis->set($game->prefix.'basedata',$nowbase);//把新的变化值缓存起来
  $val=intval($addon['basedata']/0.464)+$nowbase;
  if (isset($val)) {
    $val=$val*0.464;
    $xredis->set($game->prefix.'_now_val',$val);
    //缓存当前秒的数据60秒
    $xredis->set($game->prefix.'_biquan_time_data'.$now_time,$val,60);
  }
  self::bestorder();
  return $val;
 }
private static function xxxxxxxxxxxxxxxxxxxxxdataxxxxxxxxxxxxxxxxxxxx(){}
private static function zhandie($basedata,$dec=1,$rand_val=0){
  global $game;
  global $xredis;
   $nowbase=self::zd_rand($dec,$basedata,$rand_val);
  return $nowbase;
}
private static function zd_rand($derection=0,$basedata=0,$rand_val=0){
   global $xredis;
   global $game;
   if (!$rand_val) {
     $rand_val=mt_rand(3,9);//大波震荡
   }
    $xrand_val=mt_rand(3,9);//小波震荡
   if ($derection==0) {
     $derection=$xredis->get($game->prefix.'_derection');//方向缓存
   }
    $lit_dd=$xredis->get($game->prefix.'_lit_derection');//小波
    $dotime=$xredis->get($game->prefix.'_dotime');//连续升降次数

  switch ($derection) {
      case 1://增
         echo "\nrand_zhang++";
         if ($lit_dd==1) {
            $nowbase=$basedata-$xrand_val;
            $xx=mt_rand(1,100);
            if ($xx>65) {
              $xredis->set($game->prefix.'_lit_derection',0);
            }
         }else{
            $nowbase=$basedata+$rand_val;
            $xredis->set($game->prefix.'_lit_derection',1);
            $xredis->set($game->prefix.'_dotime',($dotime+1));
         }
        break;
      case 2://跌
         echo "\nrand_die--";
         if ($lit_dd==1) {
            $nowbase=$basedata+$xrand_val;
            $xx=mt_rand(1,100);
             if ($xx>65) {
              $xredis->set($game->prefix.'_lit_derection',0);
            }
         }else{
            $nowbase=$basedata-$rand_val;
            $xredis->set($game->prefix.'_lit_derection',1);
            $xredis->set($game->prefix.'_dotime',($dotime+1));
         }
        break;
      default:
         $xredis->set($game->prefix.'_last_derection',1);
         $xredis->set($game->prefix.'_derection',mt_rand(1,2));
         $xredis->set($game->prefix.'_dotime',0);
         $xredis->set($game->prefix.'_lit_derection',0);
        break;
    }

    //连续涨跌大于几次
    if ($dotime>=6) {
      $this_derection=mt_rand(1,2);
      $last_derection=$xredis->get($game->prefix.'_last_derection');//上局涨跌方向
      if ($last_derection==$this_derection) {
         $lastcount=$xredis->get($game->prefix.'_last_dere_count');//连续上升下降次数
         if ($lastcount>=2) {//连续n次相同方向？？
           $this_derection=$this_derection==1?2:1;
           $xredis->set($game->prefix.'_last_dere_count',0);
         }else{
           $xredis->set($game->prefix.'_last_dere_count',($lastcount+1));
         }
         $xredis->set($game->prefix.'xxx_dere_count',0);
      }else{
         $xredis->set($game->prefix.'_last_dere_count',0);
         $xxxcount=$xredis->get($game->prefix.'xxx_dere_count');//连续相反方向次数
         if ($xxxcount>=3) {
           $this_derection=$this_derection==1?2:1;
         }else{
           $xredis->set($game->prefix.'xxx_dere_count',($xxxcount+1));
         }
      }
      $xredis->set($game->prefix.'_last_derection',$this_derection);
      $xredis->set($game->prefix.'_derection',$this_derection);
      $xredis->set($game->prefix.'_dotime',0);
      $xredis->set($game->prefix.'_lit_derection',0);
    }
  return isset($nowbase)?$nowbase:'';
}

//核心函数,判断在不受崆时间内第二个单的金额是否大于第一个金额
//决策弹出
private static function bestorder(){
  global $xredis;
  global $game;
 $frist=$xredis->lRange($game->prefix.'biquan'.'_order_line',-1,-1);//最后一个
 $map=array();
 $map['dda']=$frist;
if ($frist) {
    $frist_dat=json_decode($frist[0],true);
    $second=$xredis->lRange($game->prefix.'biquan'.'_order_line',-2,-2);//倒数第二个
    if ($second) {
      $second_dat=json_decode($second[0],true);
        if ($second_dat) {
            $cha=$second_dat['buytime']-$frist_dat['buytime'];
            $frist_val=$xredis->get($game->prefix.'setorder'.$frist_dat['buytime']);
            $fdat=json_decode($frist_val,true);
            if ($fdat['up_val']>=$fdat['down_val']) {
               $f_type=0;//综合压涨
               $f_cha=$fdat['up_val']-$fdat['down_val'];
            }else{
               $f_type=1;//综合压跌
               $f_cha=$fdat['down_val']-$fdat['up_val'];
            }
            $second_val=$xredis->get($game->prefix.'setorder'.$second_dat['buytime']);
            $sdat=json_decode($second_val,true);
            if ($sdat['up_val']>=$sdat['down_val']) {
               $s_type=0;//综合压涨
               $s_cha=$sdat['up_val']-$sdat['down_val'];
            }else{
               $s_type=1;//综合压跌
               $s_cha=$sdat['down_val']-$sdat['up_val'];
            }
            //如果两个对压
            if ($f_type!=$s_type) {
                if ($f_cha>=$s_cha) { //如果第一个压的比较多
                  # code...
                   $map['msg']= "\n第一个为准";
                   echo $map['msg'];
                   $map['dda']=$frist;
                }else{ //如果第二个压的比较多
                  $map['dda']=$second;
                   if ($cha<=5) {
                      $map['msg']= "\n第二个为准";
                      echo $map['msg']; 
                      print_r($map);
                      //看下前面是不是有pop过了.没有的话再pop,缓存pop出
                      $pop_time=$xredis->get($game->prefix."_poptime");
                      if (!$pop_time) {
                        $last_data=json_decode($map['dda'][0],true);
                        if ($last_data) {
                          $xredis->set($game->prefix."_poptime",$last_data['buytime']);
                        }else{
                          $xredis->set($game->prefix."_poptime",time());
                        }
                        $xredis->RPOP($game->prefix.'biquan'.'_order_line');//Pop出来,读取删除
                      } 
                   }
                  
                }
            }
        }
    }
    return;
 }
}

private static function yyyyyyyyyyyyyyyyyyyyyyyyfffyyyyyyyyyyyyyyyyyy(){}
  
private static function init_game(){
  global $game;
  global $xredis;
  $xredis=$game->xredis(1);

  self::clearn_redis();
   $xredis->ltrim($game->prefix.'bic_data',0,1);
   $addon=json_decode($xredis->get($game->prefix.'Biquan'),true);
   if(!$addon){
     $xredis->set($game->prefix.'Biquan',json_encode(get_addon_config('Biquan')));
     $addon=json_decode($xredis->get($game->prefix.'Biquan'),true);
     if (!$addon) {
       echo "\nerror:no addon please in enter game one time like: \nweb/index.php/index/biquan/index.html ";
       return;
     }
   }
   //$xredis->set($game->prefix.'_now_val',intval($addon['basedata'])+mt_rand(1,9)*46400);
  return 1;
}

//清除掉所有关于本项目的缓存
private static function clearn_redis(){
  global $xredis;
  global $game;
   $dat=$xredis->keys('*'.$game->prefix.'*');//最后一个
    foreach ($dat as $key => $value) {
      $rel=$xredis->del($value);//删除
    }
    return;
}
 
}
